<?

session_start();

require("../classCol.php");

require("../helpers/html.php");
require("../helpers/CHtml.php");
require("../helpers/javascript.php");
require("../helpers/CJavascript.php");

require("models/fetchers.php");
require("models/handlers.php");

require("../models/GHandlers.php");
require("../models/handlers.php");
require("../models/fetchers.php");
require("../models/GFetchers.php");
require("../controllers/processors.php");

$stlButton = " style='width:160;height:80;   
                      font-size:150%;  
                      font-weight:bold;' ";

//check what authoration
$UID	=	unserialize($_SESSION["serUID"]);

$thisMdl	= "SLS-RETURN";

$srcDbHdr = "USERID_HDR";
$srcDbDtl = "USERID_DTL";

$CFetchers = new CGFetchers;
$CHandlers = new CGHandlers;

$check_me_authed          = "NO_OK";
$_SESSION["modify_mode"]  = "N";

$_SESSION["reauthed"] = ($_SESSION["reauthed"] != "")? $_SESSION["reauthed"] : "N";
$_SESSION["recheck"]  = ($_SESSION["recheck"]  != "")? $_SESSION["recheck"]  : "N"; 

/**
* if view selected
*
*/
if($_POST["view"] || $_POST["criteria"]){
	
	require("viewdboredit_view.php");
}

if($_POST["modify"]){
	
	echo "Modify selected";
	
	if($_POST["radioview"] != ''){
		
		$_SESSION["temp_radioview"] = $_POST["radioview"];
	}
	
	/**
	* if job id is not null then process
	*
	*/
	if($_SESSION["reauthed"] == "N" && $_SESSION["recheck"] == "N" ){
		
		$_SESSION["recheck"] = "Y";
		
		$flsMessage = "Validity Check";
		$tgtUrl     = "viewdboredit.php";
		$btnLabel   = "Submit";
		$btnName    = "modify";
		$btnValue   = "Modify";
		
		procChkValidation($flsMessage,
                      $tgtUrl,
                      $btnLabel,
                      $btnName,
                      $btnValue,
                      $stlButton);
		
	}elseif($_SESSION["reauthed"] == "N" && $_SESSION["recheck"] == "Y"){
		
		//check to db
		$COracle = new oracleDb;
		
		$COracle->init($_SESSION["ORCLU"],
		               $_SESSION["OPASS"],
		               $_SESSION["SID"],
		               _NO_COMMENT);
		               
		if ($COracle->connect()){
			
			fetchRelogin($COracle,
			             $_POST["reauth_user"],
			             $_POST["reauth_pass"],
			             $_SESSION["company"],
			             $srcDbHdr,
			             $nrows,
			             $_SESSION["user_id_reauth"]);
			
			if($nrows > 0 ){
				
				if(($chk_res = chk_priv($COracle,
				                        "EDIT_RIGHTS",
				                        $UID->getUID(),
				                        $thisMdl,
				                        $srcDbDtl)) == "OK"){
					
					$_SESSION["reauthed"] = "Y";
					
					$flsMessage = "Login Ok";
      		$tgtUrl     = "viewdboredit.php";
      		$btnLabel   = "Continue";
      		$btnName    = "modify";
      		$btnValue   = "Modify";
		
					formOkValidated($flsMessage,
					                $tgtUrl,
					                $btnLabel,
					                $btnName,
					                $btnValue,
					                $stlButton);
					
					$COracle->dc();
					
				}else{
					
					$COracle->dc();
					
					usrError("Authed Error reason : ".$chk_res." ");
					
					//jumpTo(2,"../".$_SESSION["source_caller"]);
				}
			}else{
				
				$COracle->dc();
				
				$_SESSION["recheck"] = "Y";
				$flsMessage = "Validity Check";
    		$tgtUrl     = "viewdboredit.php";
    		$btnLabel   = "Submit";
    		$btnName    = "modify";
    		$btnValue   = "Modify";
		
    		procChkValidation($flsMessage,
                          $tgtUrl,
                          $btnLabel,
                          $btnName,
                          $btnValue,
                          $stlButton);
			}
		}else{
			
			usrDie("Oracle connection failed");
		}
	}elseif($_SESSION["reauthed"] == "Y" && $_SESSION["recheck"] == "Y"){
		
		$_SESSION["reauthed"] = "N";
		$_SESSION["recheck"]  = "N";
		
		if($_POST["radioview"] == ''){
			
			$_POST["radioview"] = $_SESSION["temp_radioview"] ;
		}
		
		$check_me_authed = "OK";
		require("viewdboredit_modify.php");	
	}else{
		
		$_SESSION["recheck"] = "Y";
		
		$flsMessage = "Validity Check";
		$tgtUrl     = "viewdboredit.php";
		$btnLabel   = "Submit";
		$btnName    = "modify";
		$btnValue   = "Modify";
		
		procChkValidation($flsMessage,
                      $tgtUrl,
                      $btnLabel,
                      $btnName,
                      $btnValue,
                      $stlButton);
	}
}

if($_POST["approve"]){
  
	echo "Approve selected.\n";
	//print_r($_POST);
	if($_POST["selbox"] != ''){
		
		$_SESSION["temp_selbox"] = $_POST["selbox"];
	}
	//if reauthed = N then prompt the user to reauth
	if($_SESSION["reauthed"] == "N" && $_SESSION["recheck"] == "N" ){
		
		$_SESSION["recheck"] = "Y";
		
		$flsMessage = "Validity Check";
		$tgtUrl     = "viewdboredit.php";
		$btnLabel   = "Submit";
		$btnName    = "approve";
		$btnValue   = "Approve";
		
		procChkValidation($flsMessage,
                      $tgtUrl,
                      $btnLabel,
                      $btnName,
                      $btnValue,
                      $stlButton);
                      
	}elseif($_SESSION["reauthed"] == "N" && $_SESSION["recheck"] == "Y"){
		
		//check to db
		$COracle = new oracleDb;
		
		$COracle->init($_SESSION["ORCLU"],
		               $_SESSION["OPASS"],
		               $_SESSION["SID"],
		               _NO_COMMENT);
		               
		if ($COracle->connect()){
			
			fetchRelogin($COracle,
			             $_POST["reauth_user"],
			             $_POST["reauth_pass"],
			             $_SESSION["company"],
			             $srcDbHdr,
			             $nrows,
			             $_SESSION["user_id_reauth"]);
			             
			if($nrows > 0 ){
				
				if(($chk_res = chk_priv($COracle,
				                        "APPROVED_RIGHTS",
				                        $UID->getUID(),
				                        $thisMdl,
				                        $srcDbDtl)) == "OK"){
					
					$_SESSION["reauthed"] = "Y";
					
					$flsMessage = "Login Ok";
      		$tgtUrl     = "viewdboredit.php";
      		$btnLabel   = "Continue";
      		$btnName    = "approve";
      		$btnValue   = "Approve";
		
					formOkValidated($flsMessage,
					                $tgtUrl,
					                $btnLabel,
					                $btnName,
					                $btnValue,
					                $stlButton);
								
					$COracle->dc();
				}else{
					
					$COracle->dc();
					
					echo "<br/><font size='16'> Authed Error reason :" .$chk_res. "
					                            <br/>You are being transfer back to front menu </font>\n";
					
					//jumpTo(2,"../".$_SESSION["source_caller"]);
				}
			}else{
				
				$_SESSION["recheck"] = "Y";
				
				$flsMessage = "Validity Check";
    		$tgtUrl     = "viewdboredit.php";
    		$btnLabel   = "Submit";
    		$btnName    = "approve";
    		$btnValue   = "Approve";
    		
    		procChkValidation($flsMessage,
                          $tgtUrl,
                          $btnLabel,
                          $btnName,
                          $btnValue,
                          $stlButton);
			}
		}else{
			
			usrDie("Oracle connection failed");
		}
	}elseif($_SESSION["reauthed"] == "Y" && $_SESSION["recheck"] == "Y"){
		
		$_SESSION["reauthed"] = "N";
		$_SESSION["recheck"]  = "N";
		
		$_POST["selbox"] = $_SESSION["temp_selbox"] ;
		
		$check_me_authed = "OK";
		require("viewdboredit_approve.php");
	}else{
		
		$_SESSION["recheck"] = "Y";
		
		$flsMessage = "Validity Check";
		$tgtUrl     = "viewdboredit.php";
		$btnLabel   = "Submit";
		$btnName    = "approve";
		$btnValue   = "Approve";
		
		procChkValidation($flsMessage,
                      $tgtUrl,
                      $btnLabel,
                      $btnName,
                      $btnValue,
                      $stlButton);
	}
}

if($_POST["cancel"] ){
	
	echo "Cancel selected.\n";
	if($_POST["selbox"] != ''){
		
		$_SESSION["temp_selbox"] = $_POST["selbox"];
	}
	//if reauthed = N then prompt the user to reauth
	if($_SESSION["reauthed"] == "N" && $_SESSION["recheck"] == "N" ){
		
		$_SESSION["recheck"] = "Y";
		
		$flsMessage = "Validity Check";
		$tgtUrl     = "viewdboredit.php";
		$btnLabel   = "Submit";
		$btnName    = "cancel";
		$btnValue   = "Cancel";
		
		procChkValidation($flsMessage,
                      $tgtUrl,
                      $btnLabel,
                      $btnName,
                      $btnValue,
                      $stlButton);
	
	}elseif($_SESSION["reauthed"] == "N" && $_SESSION["recheck"] == "Y"){
		
		//check to db
		$COracle = new oracleDb;
		
		$COracle->init($_SESSION["ORCLU"],
		               $_SESSION["OPASS"],
		               $_SESSION["SID"],
		               _NO_COMMENT);
		               
		if ($COracle->connect()){
			
			fetchRelogin($COracle,
			             $_POST["reauth_user"],
			             $_POST["reauth_pass"],
			             $_SESSION["company"],
			             $srcDbHdr,
			             $nrows,
			             $_SESSION["user_id_reauth"]);
			             
			if($nrows > 0 ){
				
				if(($chk_res = chk_priv($COracle,
				                        "DELETE_RIGHTS",
				                        $UID->getUID(),
				                        $thisMdl,
				                        $srcDbDtl)) == "OK"){
					
					$_SESSION["reauthed"] = "Y";
					
					$flsMessage = "Login Ok";
      		$tgtUrl     = "viewdboredit.php";
      		$btnLabel   = "Continue";
      		$btnName    = "cancel";
      		$btnValue   = "Cancel";
		
					formOkValidated($flsMessage,
					                $tgtUrl,
					                $btnLabel,
					                $btnName,
					                $btnValue,
					                $stlButton);
					                
					$COracle->dc();
				}else{
					
					$COracle->dc();
					
					usrError("Authed Error reason :".$chk_res." ");
					//jumpTo(2,"../".$_SESSION["source_caller"]);
				}
			}else{
				
				$_SESSION["recheck"] = "Y";
				
				$flsMessage = "Validity Check";
    		$tgtUrl     = "viewdboredit.php";
    		$btnLabel   = "Submit";
    		$btnName    = "cancel";
    		$btnValue   = "Cancel";
    		
    		procChkValidation($flsMessage,
                          $tgtUrl,
                          $btnLabel,
                          $btnName,
                          $btnValue,
                          $stlButton);
			}
		}else{
			
			usrDie("Oracle connection failed");
		}
	}elseif($_SESSION["reauthed"] == "Y" && $_SESSION["recheck"] == "Y"){
		
		$_SESSION["reauthed"] = "N";
		$_SESSION["recheck"]  = "N";
		
		if($_POST["radioview"] == ''){
			
			$_POST["selbox"] = $_SESSION["temp_selbox"] ;
		}
		$check_me_authed = "OK";
		require("viewdboredit_cancel.php");
	}else{
		
		$_SESSION["recheck"] = "Y";
		
		$flsMessage = "Validity Check";
		$tgtUrl     = "viewdboredit.php";
		$btnLabel   = "Submit";
		$btnName    = "cancel";
		$btnValue   = "Cancel";
		
		procChkValidation($flsMessage,
                      $tgtUrl,
                      $btnLabel,
                      $btnName,
                      $btnValue,
                      $stlButton);
	}
}

if($_POST["delete"]){
	
	echo "delete selected.\n";
	if($_POST["selbox"] != ''){
		
		$_SESSION["temp_selbox"] = $_POST["selbox"];
	}
	//if reauthed = N then prompt the user to reauth
	if($_SESSION["reauthed"] == "N" && $_SESSION["recheck"] == "N" ){
		
		$_SESSION["recheck"] = "Y";
		
		$flsMessage = "Validity Check";
		$tgtUrl     = "viewdboredit.php";
		$btnLabel   = "Submit";
		$btnName    = "delete";
		$btnValue   = "Delete";
		
		procChkValidation($flsMessage,
                      $tgtUrl,
                      $btnLabel,
                      $btnName,
                      $btnValue,
                      $stlButton);
                      
	}elseif($_SESSION["reauthed"] == "N" && $_SESSION["recheck"] == "Y"){
		
		//check to db
		$COracle = new oracleDb;
		
		$COracle->init($_SESSION["ORCLU"],
		               $_SESSION["OPASS"],
		               $_SESSION["SID"],
		               _NO_COMMENT);
		               
		if ($COracle->connect()){
			
			fetchRelogin($COracle,
			             $_POST["reauth_user"],
			             $_POST["reauth_pass"],
			             $_SESSION["company"],
			             $srcDbHdr,
			             $nrows,
			             $_SESSION["user_id_reauth"]);
			             
			if($nrows > 0 ){
				
				if(($chk_res = chk_priv($COracle,
				                        "DELETE_RIGHTS",
				                        $UID->getUID(),
				                        $thisMdl,
				                        $srcDbDtl)) == "OK"){
					
					$_SESSION["reauthed"] = "Y";
					
					$flsMessage = "Login Ok";
      		$tgtUrl     = "viewdboredit.php";
      		$btnLabel   = "Continue";
      		$btnName    = "delete";
      		$btnValue   = "Delete";
		
					formOkValidated($flsMessage,
					                $tgtUrl,
					                $btnLabel,
					                $btnName,
					                $btnValue,
					                $stlButton);
					                
					$COracle->dc();
				}else{
				  
				  $COracle->dc();
				  
					usrError("Authed Error reason :" .$chk_res. " ");
					//jumpTo(2,"../".$_SESSION["source_caller"]);
				}
				
			}else{
				
				$_SESSION["recheck"] = "Y";
				
				$flsMessage = "Validity Check";
    		$tgtUrl     = "viewdboredit.php";
    		$btnLabel   = "Submit";
    		$btnName    = "delete";
    		$btnValue   = "Delete";
    		
    		procChkValidation($flsMessage,
                          $tgtUrl,
                          $btnLabel,
                          $btnName,
                          $btnValue,
                          $stlButton);
			}
		}else{
			
			usrDie("Oracle connection failed");
		}
	}elseif($_SESSION["reauthed"] == "Y" && $_SESSION["recheck"] == "Y"){
		
		$_SESSION["reauthed"] = "N";
		$_SESSION["recheck"]  = "N";
		
		if($_POST["radioview"] == ''){
			$_POST["selbox"] = $_SESSION["temp_selbox"] ;
		}
		$check_me_authed = "OK";
		require("viewdboredit_delete.php");
	}else{
		
		$_SESSION["recheck"]  = "Y";
		
		$flsMessage = "Validity Check";
		$tgtUrl     = "viewdboredit.php";
		$btnLabel   = "Submit";
		$btnName    = "cancel";
		$btnValue   = "Cancel";
		
		procChkValidation($flsMessage,
                      $tgtUrl,
                      $btnLabel,
                      $btnName,
                      $btnValue,
                      $stlButton);
	}
}


if($_POST["entry"]){
	
	/**
	* The old version was
	* included with these logics
	*
	*/
  //$_SESSION["selbox"] = $_POST["selbox"];
  //var_dump($_POST);
	//session_write_close();
	
	jumpTo(0,"entry.php");
}

if($_POST["back"]){
	
	/**
	* Since this module is special case
	* then we pass the jumpTo with additional
	* variable
	*
	*/
	jumpTo(0,"../".$_SESSION["source_caller"]."?transVariable=1");
}
?>